<?php

namespace addons\third\controller;

use addons\third\library\Application;
use app\admin\model\exam\ConfigInfoModel;
use app\common\controller\Api as commonApi;
use addons\third\library\Service;
use addons\third\model\Third;
use app\common\library\Sms;
use fast\Random;
use think\Config;
use think\Session;
use think\Validate;

/**
 * 第三方登录插件
 */
class Api extends commonApi
{
    protected $noNeedLogin = ['getAuthUrl', 'callback', 'account', 'getOpenidCallback']; // 无需登录即可访问的方法，同时也无需鉴权了
    protected $noNeedRight = ['*']; // 无需鉴权即可访问的方法

    protected $app = null;
    protected $options = [];
    protected $config = null;

    public function _initialize()
    {
        //跨域检测
        check_cors_request();
        //设置session_id
        Config::set('session.id', $this->request->server("HTTP_SID"));

        parent::_initialize();
        $this->config = get_addon_config('third');
        $name = $this->request->request('addon');
        if (!$name && get_addon_info($name)) {
            //判断是否有插件标识，如果有插件标识则取指定插件中的配置
            $config = get_addon_config($name);
            $this->config = array_merge($this->config, array_intersect_key($config, array_flip(['qq', 'wechat', 'wechatweb', 'weibo'])));
        }
        $this->app = new Application($this->config);
    }

    /**
     * H5获取授权链接
     */
    public function getAuthUrl()
    {
        $url = $this->request->param('url', '', 'trim');
        $scope = $this->request->param('scope', 'snsapi_userinfo', 'trim');
        $platform = $this->request->param('platform','wechat');
        $parent_id = $this->request->param('parent_id',0);
        if(!$url){

            $config = ConfigInfoModel::where(['id'=>1])->value('system_config');
            $config = json_decode($config, true);
            $url = $config['url'].'/addons/third/Api/callback?platform=wechat&apptype=mp&parent_id='.$parent_id;
        }
        if (!$url || !$platform || !isset($this->config[$platform])) {
            $this->error('参数错误');
        }
        $this->config[$platform]['callback'] = $url;

        //判断是否静默授权
        if ($scope) {
            $this->config[$platform]['scope'] = $scope;
        }
        $this->app = new Application($this->config);
        if (!$this->app->{$platform}) {
            $this->error(__('参数错误'));
        }
        $this->success('', $this->app->{$platform}->getAuthorizeUrl());
    }

    /**
     * 公众号授权回调的请求
     */
    public function callback()
    {
        $platform = $this->request->param('platform');
        $apptype = $this->request->param('apptype');
        $parent_id = $this->request->param('parent_id',0);
//        $base = $this->request->param('base', 0); //只返回基础信息
//        $bind = $this->request->param('bind', 1); //默认要求绑定账号

        if (!$this->app->{$platform}) {
            $this->error(__('参数错误'));
        }
        $userinfo = $this->app->{$platform}->getUserInfo($this->request->param('', null, null));
        if (!$userinfo) {
            $this->error(__('操作失败'));
        }
            $config = ConfigInfoModel::where(['id'=>1])->value('system_config');
            $config = json_decode($config, true);
//        if ($base) {

//
//            header('Location: ' . $config['url']  . '/h5#/pages/public/loading/?token=' . $userinfo['token']);
////            $this->success("授权成功！", $userinfo['userinfo']);
//        }

        $userinfo['apptype'] = $apptype ?: Service::getApptype();
        $userinfo['platform'] = $platform;

//        $third = [
//            'avatar'   => $userinfo['userinfo']['avatar'],
//            'nickname' => $userinfo['userinfo']['nickname']
//        ];
//        if ($this->auth->isLogin() || Service::isBindThird($userinfo['platform'], $userinfo['openid'], $userinfo['apptype'], $userinfo['unionid'])) {
//            $result = Service::connect($userinfo['platform'], $userinfo);
//            if (!$result) {
//                $this->error('授权登录失败');
//            }
//            $user = $this->auth->getUserinfo();
//        } else {
//            $user = false;
//            Session::set('third-userinfo', $userinfo);
//        }
        $extend['parent_id'] = $parent_id;
        $result = Service::connect($userinfo['platform'], $userinfo,$extend);
        if (!$result) {
            $this->error('授权登录失败');
        }
        $user = $this->auth->getUserinfo();
        if(!$user){
            header('Location: ' . $config['url']  . '/h5#/pages/public/loading');
        }
        header('Location: ' . $config['url']  . '/h5#/pages/public/loading/?token=' . $user['token']);
//        $this->success("授权成功！", ['user' => $user, 'third' => $third, 'openid' => $userinfo['openid']]);
    }

//    public function jssdk()
//    {
//        $params = $this->request->post();
//        $apis = [
//            'checkJsApi',
//            'updateTimelineShareData',
//            'updateAppMessageShareData',
//            "onMenuShareAppMessage",
//            "onMenuShareTimeline",
//            'getLocation', //获取位置
//            'openLocation', //打开位置
//            'scanQRCode', //扫一扫接口
//            'chooseWXPay', //微信支付
//            'chooseImage', //拍照或从手机相册中选图接口
//            'previewImage', //预览图片接口       'uploadImage', //上传图片
//            'openAddress',   // 获取微信地址
//        ];
//        // $openTagList = [
//        //     'wx-open-subscribe'
//        // ];
//
//        $uri = urldecode($params['uri']);
//
//        $wechat = new WechatLibrary('wxOfficialAccount');
//
//        $jssdk = $wechat->getApp()->jssdk->setUrl($uri);
//        // easywechat 版本 < 4.2.33 的 buildConfig 方法 没有 openTagList 参数，手动覆盖底层 buildConfig 方法
//        $res = $wechat->buildConfig($jssdk, $apis, $debug = false, $beta = false, $json = false);
//
//        $this->success('sdk', $res);
//    }


    /**
     * 获取Openid回调
     */
    public function getOpenidCallback()
    {
        $platform = $this->request->param('platform');
        if (!$this->app->{$platform}) {
            $this->error(__('参数错误'));
        }
        $userinfo = $this->app->{$platform}->getUserInfo($this->request->param('', null, null));
        if (!$userinfo) {
            $this->error(__('操作失败'));
        }

        $this->success('', ['openid' => $userinfo['openid']]);
    }

    /**
     * 登录或创建账号
     */
    public function account()
    {
        if ($this->request->isPost()) {
            $params = Session::get('third-userinfo');
            $mobile = $this->request->post('mobile', '');
            $code = $this->request->post('code', $this->request->post('captcha'));
            $token = $this->request->post('__token__');
            $rule = [
                'mobile'    => 'require|regex:/^1\d{10}$/',
                '__token__' => 'require|token',
            ];
            $msg = [
                'mobile' => 'Mobile is incorrect',
            ];
            $data = [
                'mobile'    => $mobile,
                '__token__' => $token,
            ];
            $ret = Sms::check($mobile, $code, 'bind');
            if (!$ret) {
                $this->error(__('验证码错误'));
            }
            $validate = new Validate($rule, $msg);
            $result = $validate->check($data);
            if (!$result) {
                $this->error(__($validate->getError()), ['__token__' => $this->request->token()]);
            }

            $userinfo = \app\common\model\User::where('mobile', $mobile)->find();
            if ($userinfo) {
                $result = $this->auth->direct($userinfo->id);
            } else {
                $result = $this->auth->register($mobile, Random::alnum(), '', $mobile, $params['userinfo'] ?? []);
            }

            if ($result) {
                $result = Service::connect($params['platform'], $params);
                if (!$result) {
                    $this->error('授权登录失败');
                }
                $this->success(__('绑定账号成功'), ['userinfo' => $this->auth->getUserinfo()]);
            } else {
                $this->error($this->auth->getError(), ['__token__' => $this->request->token()]);
            }
        }
    }

    /**
     * 获取第三方登录绑定列表
     */
    public function getBindList()
    {
        $thirdList = Third::where('user_id', $this->auth->id)->field('user_id,platform,apptype')->select();
        $this->success('', collection($thirdList)->toArray());
    }


    /**
     * 解绑
     */
    public function unbind()
    {
        $platform = $this->request->post("platform");
        $apptype = $this->request->post("apptype");
        $exist = Third::where('user_id', $this->auth->id)->where('platform', $platform)->where('apptype', $apptype)->find();
        if ($exist) {
            $this->success("解绑成功");
        } else {
            $this->error("未找到相关数据");
        }
    }

}
